import axios from 'axios'
import commonUrl from '../common/js/commonUrl.js'
import {loadOrderCityId} from '../common/js/cache.js'
const url = `${commonUrl.apihost}index.php/home/city`
// 城市获取
export function getOrderCity () {
  return axios({
    method: 'GET',
    url: `${url}/city`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 根据城市，获取该城市区域
export function getStreets (cityId) {
  return axios({
    method: 'GET',
    url: `${url}/district?cityId=${cityId}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 默认地区
export function getDefault () {
  return axios({
    method: 'GET',
    // url: `${url}/district?cityId=${loadOrderCityId()}`
    url: `${url}/district?cityId=3`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 点击区域返回小区
export function getVillage2 (paramsId) {
  return axios({
    method: 'GET',
    url: `${url}/village?districtId=${paramsId}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 获取小区列表
export function getVillageLoad () {
  return axios({
    method: 'GET',
    url: `${url}/village2?districtId=${loadOrderCityId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 获取小区列表带字母A
export function getVillages(paramsId) {
  return axios({
    method: 'GET',
    url: `${url}/village2?districtId=${paramsId}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
// 搜索小区
export function searchKeyword2 (keyword) {
  return axios({
    method: 'GET',
    url: `${url}/search?key=${keyword}&cityId=${loadOrderCityId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

export function searchKeywords (keyword) {
  return axios({
    method: 'GET',
    url: `${url}/search2?key=${keyword}&cityId=${loadOrderCityId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
export function submitArticle (title, author, tag, fileId, content, token) {
  const data = {
    title,
    author,
    tag,
    fileId,
    content
  }

  return axios({
    method: 'POST',
    url,
    headers: {
      Authorization: `Bearer ${token}\n`
    },
    data
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
